import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { _HttpClient, SettingsService } from '@delon/theme';
import * as moment from 'moment';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { zip } from 'rxjs';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'app-business-order-view',
  templateUrl: './view.component.html'
})
export class BusinessOrderViewComponent implements OnInit {
  i: any;
  overview: any;
  itemOverview: any;
  itemCurrent: any;
  isAdmin = false;

  statusNotice = {
    '3': { index: 3, text: '已发货', notice: '配送中', color: 'blue' },
    '5': { index: 5, text: '交易成功', notice: '订单完成', color: 'green' },
    '6': { index: 6, text: '已拒签', notice: '', color: '#f50' },
    '7': { index: 7, text: '已拒签', notice: '', color: '#f50' },
    '8': { index: 8, text: '待补货', notice: '确保 Fordeal 仓库在 7 天内有足够库存发货', color: 'orange' },
    '9': { index: 9, text: '补货中', notice: '卖家发货至 Fordeal 履约仓途中', color: 'gold' },
    '10': { index: 10, text: '处理中', notice: '订单库存已匹配，Fordeal 履约仓处理中，即将发出', color: 'cyan' },
    '99': { index: 99, text: '已取消', notice: '', color: 'red' }
  };
  skuStatusNotice = {
    '3': { index: 3, text: '已发货', notice: '配送中', color: 'blue' },
    '5': { index: 5, text: '交易成功', notice: '订单完成', color: 'green' },
    '6': { index: 6, text: '已拒签', notice: '', color: '#f50' },
    '7': { index: 7, text: '已拒签', notice: '', color: '#f50' },
    '8': { index: 8, text: '库存不足', notice: 'Fordeal 仓库无法发货，等待商家补充库存中', color: 'orange' },
    '9': { index: 9, text: '备货在途', notice: '发往 Fordeal 仓库途中', color: 'gold' },
    '10': { index: 10, text: '库存已匹配', notice: 'Fordeal 仓库中已有库存', color: 'cyan' },
    '99': { index: 99, text: '已取消', notice: '', color: 'red' }
  };
  deadline: any;

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef,
    private settingService: SettingsService
  ) {
    if (this.settingService.user.ID == 1) {
      this.isAdmin = true;
    }
  }

  ngOnInit(): void {
    if (this.i.orderSn != '') {
      this.loadDataFromServer(this.i.shopId, this.i.orderSn);
    }
  }

  loadDataFromServer(shopId: number, orderSn: string) {
    zip(
      this.http.get(`api/order/overview`, { shopId: shopId, orderSn: orderSn }),
      this.http.get(`api/order/item_overview`, { shopId: shopId, orderSn: orderSn }),
      this.http.get(`api/order/item_current`, { shopId: shopId, orderSn: orderSn })
    )
      .pipe(
        finalize(() => {
          this.cdr.detectChanges();
        })
      )
      .subscribe(([overview, itemOverview, itemCurrent]: [NzSafeAny, NzSafeAny, NzSafeAny]) => {
        // this.i = true;
        this.overview = overview.data;
        this.deadline = moment(overview.data['paymentAt']).add(5, 'days');
        // 订单明细优惠逻辑
        for (let j = 0; j < itemOverview.data['skus'].length; j++) {
          let quantityFeeInfoList = itemOverview.data['skus'][j]['quantityFeeInfoList'];
          let settlePrice = itemOverview.data['skus'][j]['finalPrice'];
          for (let k = 0; k < quantityFeeInfoList.length; k++) {
            let quantityFeeInfo = quantityFeeInfoList[k];
            console.log(settlePrice, quantityFeeInfo['frees']);
            settlePrice += quantityFeeInfo['frees'];
          }

          itemOverview.data['skus'][j]['settlePrice'] = settlePrice;
        }
        this.itemOverview = itemOverview.data;
        this.itemCurrent = itemCurrent.data;
      });
  }

  loadItemCurrent(orderSn: string, recordId: number): void {
    this.http.get(`api/order/item_current`, { orderSn: orderSn, recordId: recordId }).subscribe(res => {
      let elements = document.getElementsByClassName('ti');
      for (let i = 0; i < elements.length; i++) {
        elements[i].setAttribute('style', 'color:#13C2C2');
      }
      let element = document.getElementById(`ti_${recordId}`);
      // @ts-ignore
      element.style.color = 'blue';
      // 订单明细优惠逻辑
      for (let j = 0; j < res.data['skus'].length; j++) {
        let quantityFeeInfoList = res.data['skus'][j]['quantityFeeInfoList'];
        let settlePrice = res.data['skus'][j]['finalPrice'];
        for (let k = 0; k < quantityFeeInfoList.length; k++) {
          let quantityFeeInfo = quantityFeeInfoList[k];
          console.log(settlePrice, quantityFeeInfo['frees']);
          settlePrice += quantityFeeInfo['frees'];
        }

        res.data['skus'][j]['settlePrice'] = settlePrice;
      }
      this.itemCurrent = res.data;
    });
  }

  close(): void {
    this.modal.destroy();
  }
}
